package org.shiro.service.Impl;

import org.shiro.dao.RoleMapper;
import org.shiro.entity.Role;
import org.shiro.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
@Service
public class RoleServiceImpl implements RoleService {
  @Autowired private RoleMapper roleMapper;

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public int create(Role role) {
    Assert.notNull(role, "参数错误");
    int res = roleMapper.insertSelective(role);
    if (res > 0) {
      return res;
    }
    return 0;
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public int remove(Long id) {
    Assert.notNull(id, "参数错误");
    int res = roleMapper.deleteByPrimaryKey(id);
    if (res > 0) {
      return res;
    }
    return 0;
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public Role load(Long id) {
    Assert.notNull(id, "参数错误");
    Role role = roleMapper.selectByPrimaryKey(id);
    if (role != null) {
      return role;
    }
    return null;
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public int modity(Role role) {
    Assert.notNull(role, "参数错误");
    int res = roleMapper.updateByPrimaryKeySelective(role);
    if (res > 0) {
      return res;
    }
    return 0;
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public List<Role> selectAll() {
    List<Role> roleList = roleMapper.selectAll();
    if (roleList != null && roleList.size() > 0) {
      return roleList;
    }
    return null;
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public List<Role> queryByLimit(int offset, int limit) {
    Assert.notNull(offset, "参数错误");
    Assert.notNull(limit, "参数错误");
    List<Role> roles = roleMapper.queryByLimit(offset, limit);
    if (roles != null && roles.size() > 0) {
      return roles;
    }
    return null;
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public List<Role> selectByUserId(Long id) {
    Assert.notNull(id, "参数错误");
    List<Role> roleList = roleMapper.selectByUserId(id);
    if (roleList != null && roleList.size() > 0) {
      return roleList;
    }
    return null;
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public Role queryByName(String rolename) {
    Assert.notNull(rolename, "参数错误");
    Role role = roleMapper.queryByRoleName(rolename);
    if (role != null) {
      return role;
    }
    return null;
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public List<Role> queryMenu(String rolename) {
    Assert.notNull(rolename, "参数错误");
    Role role = roleMapper.queryByRoleName(rolename);
    if (role == null) {
      throw new IllegalArgumentException("当前系统无该角色，请先添加该角色");
    }
    List<Role> roleList = roleMapper.queryMenu(role.getId());
    if (roleList.isEmpty() || roleList.size() <= 0) {
      throw new IllegalArgumentException("当前表中无数据");
    }
    return roleList;
  }
}
